package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.customer.domain.Customer;
import com.ktgj.customer.domain.vo.MallCartVO;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.customer.domain.MallCartProduct;
import com.ktgj.customer.service.IMallCartProductService;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 购物车商品Controller
 * 
 * @author ruoyi
 * @date 2022-05-30
 */
@RestController
@RequestMapping("/MallCartProduct")
public class MallCartProductController extends BaseController
{
    @Autowired
    private IMallCartProductService mallCartProductService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询购物车商品列表
     */
    @PreAuthorize(hasPermi = "customer:product:list")
    @GetMapping("/list")
    public TableDataInfo list(MallCartProduct mallCartProduct)
    {
        startPage();
        List<MallCartProduct> list = mallCartProductService.selectMallCartProductList(mallCartProduct);
        return getDataTable(list);
    }

    /**
     * 导出购物车商品列表
     */
    @PreAuthorize(hasPermi = "customer:product:export")
    @Log(title = "购物车商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallCartProduct mallCartProduct) throws IOException
    {
        List<MallCartProduct> list = mallCartProductService.selectMallCartProductList(mallCartProduct);
        ExcelUtil<MallCartProduct> util = new ExcelUtil<MallCartProduct>(MallCartProduct.class);
        util.exportExcel(response, list, "购物车商品数据");
    }

    /**
     * 获取购物车商品详细信息
     */
    @PreAuthorize(hasPermi = "customer:product:query")
    @GetMapping(value = "/{cartProductId}")
    public AjaxResult getInfo(@PathVariable("cartProductId") Long cartProductId)
    {
        return AjaxResult.success(mallCartProductService.selectMallCartProductById(cartProductId));
    }

    /**
     * 新增购物车商品
     */
    @PreAuthorize(hasPermi = "customer:product:add")
    @Log(title = "购物车商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MallCartProduct mallCartProduct)
    {
        return toAjax(mallCartProductService.insertMallCartProduct(mallCartProduct));
    }

    /**
     * 修改购物车商品
     */
    @PreAuthorize(hasPermi = "customer:product:edit")
    @Log(title = "购物车商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MallCartProduct mallCartProduct)
    {
        return toAjax(mallCartProductService.updateMallCartProduct(mallCartProduct));
    }

    /**
     * 删除购物车商品
     */
    @PreAuthorize(hasPermi = "customer:product:remove")
    @Log(title = "购物车商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{cartProductIds}")
    public AjaxResult remove(@PathVariable Long[] cartProductIds)
    {
        return toAjax(mallCartProductService.deleteMallCartProductByIds(cartProductIds));
    }

    /**
     * 添加购物车
     * @param request
     * @param mallCartProduct
     * @return
     */
    @PostMapping("/addMallCartProduct")
    public AjaxResult addMallCartProduct(HttpServletRequest request, @RequestBody MallCartProduct mallCartProduct) {
        Customer customer;
        try {
            customer = loginService.getLoginCustomer(request);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
        if (customer != null) {
            int i = 0;
            if (customer.getOpenId() != null)
                i = mallCartProductService.addMallCartProduct(customer.getCustomerId(), customer.getOpenId(), mallCartProduct);
            else
                i = mallCartProductService.addMallCartProduct(customer.getCustomerId(), null, mallCartProduct);
            return toAjax(i);
        } else {
            return AjaxResult.error("01002", "登录已过期");
        }
    }
}
